<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" :searchImg="false" />
    <el-card shadow="never" :body-style="{ padding: '10px 10px 0' }" class="mt10">
      <div slot="header" class="f16">
        <span style="font-weight: 600" class="mr55">扫描采购单条码 <span class="f14 red">PS:扫描指令单标签时，请确认光标置于此栏位</span></span>
      </div>
      <div class="flex mt10 f14" style="align-items: center; justify-content: space-evenly">
        <div>
          <span class="mr10">扫描条码</span>
          <el-input @keydown="(event:any) => handleKeyDown(event)" v-model="scanCode" style="width: 330px; height: 25px; font-size: 20px" />
        </div>
        <div class="flex AIC">
          <span class="mr10">扫描信息</span>
          <div :class="flagBC === '0' ? '' : flagBC === '1' ? 'susses' : 'error'" style="width: 250px; height: 25px; border: 1px solid black">
            {{ scanMessage }}
          </div>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '0px 10px 0' }">
      <div slot="header" class="f16">
        <span style="font-weight: 600" class="mr55">扫描单据资料</span>
      </div>
      <Table
        :tableHeader="tableHeader"
        :tableData="tableData"
        class="mt10"
        :TableHeight="40"
        :outTableHeight="180"
        :isLoading="isLoading2"
        :summaryData="summaryData"
        summary
        @currentRow="currentRow"
        @selectList="selectTableList"
        selection
      />
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '10px' }">
      <div slot="header" class="f16">
        <span style="font-weight: 600">已入库资料</span>
      </div>
      <Table
        :tableHeader="tableHeader2"
        :tableData="tableData2"
        class="mt10"
        :TableHeight="60"
        :outTableHeight="180"
        :isLoading="isLoading"
        :summaryData="summaryData"
        summary
        @currentRow="currentRow2"
      />
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, onUnmounted, ref, watch } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { ElMenu, ElMessage } from 'element-plus'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialStorage'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { selectList, selectSomeList } from '@/plugins/mixin'
import service from '@/utils/request'
import { debounce } from 'lodash'
import Find from '@/components/Find/index.vue'
const scanCode = ref('')
const flagBC = ref('0')
const scanMessage = ref('')

const handleKeyDown = async (e: any) => {
  if (e.key === 'Enter' || e.keyCode === 13) {
    const res = (await LoginWork.get(`/stock/in/scanInPcMtlIn?slipNo=${scanCode.value}`)) as any
    if (res.code === 200) {
      JMessage.success('扫描成功')
      getTableList()
      const tempCode = JSON.parse(JSON.stringify(scanCode.value))
      scanCode.value = ''
      flagBC.value = '1'
      scanMessage.value = tempCode
    } else {
      JMessage.error(res.message)
      flagBC.value = '2'
      scanMessage.value = res.message
    }
  }
}

const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await LoginWork.get<any, any>('/stock/in/getScanInSlipTemp')
  isLoading.value = false
  tableData.value = tableDataList.data.map((item: any) => {
    item.change = 'Y'
    if (item.warehouseNo === null) item.warehouseNo = '在制仓'
    item.sendDate = formatCurrentDate(new Date())
    return item
  })
  currentRow()
}, 500)
const formatCurrentDate = (date?: any) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  let newMonth = month > 10 ? month : '0' + month
  let newDay = day > 10 ? day : '0' + day
  return `${year}-${newMonth}-${newDay}`
}
onMounted(() => {
  getTableList()
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入入库单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectP: '厂商代码 厂商名称',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '收货日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入颜色',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  },
  {
    type: 'button',
    checkboxLabel: '删除入库资料当前行'
  }
])
const formList2 = ref([
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectP: '库存位置',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  },
  {
    type: 'button',
    checkboxLabel: '更新单据资料'
  },
  {
    type: 'button',
    checkboxLabel: '生成入库单'
  }
  // 不用
  // {
  //   type: 'button',
  //   checkboxLabel: '打印材料标签'
  // },
]) as any
const tableHeader = ref([
  {
    change: true,
    label: '订购单号',
    prop: 'slipNo',
    width: '160px'
  },
  {
    change: true,
    label: '订购日期',
    prop: 'slipDate'
  },
  {
    change: true,
    label: '订单',
    width: '200px',
    prop: 'orderId'
  },
  {
    change: true,
    label: '颜色',
    width: '256px',
    prop: 'mtlColor'
  },
  {
    change: true,
    label: '材料名称',
    width: '110px',
    prop: 'mtlName'
  },
  {
    change: true,
    label: '规格',
    width: '110px',
    prop: 'mtlSpec'
  },
  {
    change: true,
    label: '单位',
    width: '110px',
    prop: 'mtlUnit'
  },
  {
    change: true,
    label: '订购数量',
    prop: 'qty'
  },
  {
    change: true,
    label: '厂商',
    prop: 'vendorName'
  },
  {
    change: true,
    label: '已入数量',
    prop: 'isInQty'
  },
  {
    change: true,
    label: '未入数量',
    prop: 'unInQty'
  },
  {
    change: true,
    label: '入库日期',
    prop: 'sendDate'
  },
  {
    label: '入库数量',
    prop: 'inStockQty'
  },
  {
    change: true,
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    change: true,
    label: '仓库别',
    prop: 'warehouseNo'
  },
  {
    change: true,
    label: '库存位置',
    prop: 'areaNo'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
const tableData = ref([])

// 表一模块
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '删除入库资料当前行') {
    const res = await LoginWork.post<any, any>('/stock/in/deleteBIStorage', currentTable2Row.value)
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.warning(res.message)
    }
  }
  getTableList2()
}
const findClicked2 = async (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '更新单据资料') {
    // BFPM2024090002
    tableData.value.forEach((item: any) => {
      item.areaNo = formList2.value[0].selectValue
    })
    JMessage.success('更新成功')
  }
  if (content === '生成入库单') {
    if (selectedLists.value.length === 0) {
      JMessage.warning('请勾选单据资料')
      return
    }
    if (selectedLists.value.some((item: any) => item.vendorName === null || item.areaNo === null || item.vendorName === '' || item.areaNo === '')) {
      JMessage.warning('厂商和库存位置不能为空')
      return
    }
    const res = await LoginWork.post<any, any>('/stock/genericSlipNo', selectedLists.value)
    if (res.code === 200) {
      JMessage.success(res.message)
      getTableList()
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
}
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList2()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList2()
}
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 11,
    num: 0
  }
])
const tableHeader2 = ref([
  {
    label: '入库单号',
    prop: 'slipNo',
    width: 150
  },
  {
    label: '入库日期',
    prop: 'slipDate'
  },
  {
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    label: '订购单号',
    prop: 'purNo',
    width: 150
  },
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '订单',
    prop: 'lotNo',
    width: 150
  },
  {
    label: '颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    width: 170,
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '入库数量',
    prop: 'inQty'
  },
  {
    label: '仓库别',
    prop: 'warehouseNo'
  },
  {
    label: '库存位置',
    prop: 'areaNo'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '打印标志',
    prop: 'printFlag',
    type: 'checkbox'
  },
  {
    label: '打印日期',
    prop: 'printDate'
  }
])
const isLoading2 = ref(false)
const tableData2 = ref([])
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/stock/in/mtlBIStorageList', {
    mtlColor: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入颜色'
    )?.inputValue,
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入名称'
    )?.inputValue,
    purNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
    )?.inputValue,
    slipEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
    )?.dateValue?.[1],
    slipNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入入库单号'
    )?.inputValue,
    slipStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
    )?.dateValue?.[0],
    vendorName: formList.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称'
    )?.selectValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading2.value = false
  tableData2.value = tableDataList.list.map((item: any) => {
    if (item.printFlag === 'Y') {
      item.printFlag = true
    }
    return item
  })
  total.value = tableDataList.total
  summaryData.value[0].num = tableDataList.inTotal
}, 500)
onMounted(() => {
  getTableList2()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList2()
  }
)

// 下拉框请求数据
const getSelectList = async () => {
  // await selectList(formList, () => service.get('/pcOrder/getPcOrderDataRsjCondition'))
  await selectSomeList(formList2, () => service.get('/back/vendorMoldQuery'))
  await selectSomeList(formList, () => service.get('/back/vendorMoldQuery'))
}
const getPosition = async () => {
  let tableDataList = await service.post<any, any>('/stock/wareHouseList', {
    areaNo: '',
    areaType: '',
    remark: '',
    stkNo: '',
    warehouseNo: ''
  })
  tableDataList.forEach((item: any) => {
    formList2.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '库存位置')
      .selectOptions.push({
        optionKey: item.areaNo,
        optionLabel: item.areaNo,
        optionValue: item.areaNo
      })
  })
}
onMounted(() => {
  getSelectList()
  getPosition()
})
const currentTableRow = ref()
const currentRow = (row: any = tableData.value[0]) => {
  currentTableRow.value = row
}
const currentTable2Row = ref()
const currentRow2 = (row: any) => {
  row.printFlag === true ? (row.printFlag = 'Y') : null
  currentTable2Row.value = JSON.parse(JSON.stringify(row))
}
const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}

onUnmounted(async () => {
  await service.get('/stock/deleteSlipInTemp')
})
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-child(1)) {
  .el-select {
    width: 135px;
    height: 28px;
  }
}

:deep(.el-form-item:nth-child(3)) {
  .el-select {
    width: 135px;
    height: 28px;
  }
}
.susses {
  background-color: rgb(71, 232, 71);
  text-align: center;
  line-height: 25px;
  font-size: 20px;
}
.error {
  background-color: #ff0000;
  font-size: 20px;
  text-align: center;
}
</style>
